//
//  DataRefreshTableViewController.m
//  UESTCPioneer
//
//  Created by 马君 on 14-4-16.
//  Copyright (c) 2014年 Sway. All rights reserved.
//

#import "DataRefreshTableViewController.h"
#import "Mapping.h"
#import "PartyVedioDownloadEntity.h"
#import "PartyDataLearnEntity.h"
@interface DataRefreshTableViewController ()

@end

@implementation DataRefreshTableViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)_loadUPTableView{
    self.tableView=[[UPTableView alloc] initWithFrame:CGRectMake(0, 0, 320,self.view.frame.size.height-65) style:UITableViewStyleGrouped];
    self.tableView.tag = 27;
    [self.view addSubview:self.tableView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)pullDownRefresh:(MJRefreshBaseView *)refreshView{
    NSLog(@"90193090385923849588723475823758723845234958397483275832784573");
    [PublicMethod ClearEntity:entityName];
    NSLog(@"requestData = %@,entityName = %@,entityMapping = %@",requestData,entityName,entityMapping);
    [NetworkCenter RKRequestWithData:requestData EntityName:entityName Mapping:entityMapping SuccessBlock:^(NSArray *resultArray) {
        NSLog(@"resultArray = %@",resultArray);
        NSMutableArray *dic= [[NSMutableArray alloc]init];
        [dic addObjectsFromArray:resultArray];
        
        tableViewEntitiesArray=[[NSArray alloc]initWithArray:dic];
        
        NSLog(@"tableViewEntitiesArray = %@",tableViewEntitiesArray);
        
        if (self.isVideo) {
            [self changeStatus:@"downloadvedio.plist"];
        }
        
        if (self.isFile) {
            [self changeStatus:@"downloadfile.plist"];
        }
        
        if (self.isMajor) {
            [self changeStatus:@"downloadmajor.plist"];
        }
        [self.tableView reloadData];
        PullUpRefreshTimes=0;
        [refreshView endRefreshing];
    } failure:^(NSError *error) {
        [Alert showAlert:@"下拉刷新失败!"];
        [refreshView endRefreshing];
    }];
}

-(void)pullUpRefresh:(MJRefreshBaseView *)refreshView{
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithDictionary:requestData];
    [dic setObject:[NSString stringWithFormat:@"%d",PullUpRefreshTimes+2] forKey:@"page"];
    
    [NetworkCenter RKRequestWithData:dic EntityName:entityName Mapping:entityMapping SuccessBlock:^(NSArray *resultArray) {
        
        
        if ([resultArray count]==0){
            [refreshView endRefreshing];
            return ;
        }
        
        NSMutableArray *newArray = [[NSMutableArray alloc]init];
        [newArray addObjectsFromArray:tableViewEntitiesArray];
        [newArray addObjectsFromArray:resultArray];
        NSArray *result = [[NSArray alloc]initWithArray:newArray];
        
        
        tableViewEntitiesArray=result;
        
        if (self.isVideo) {
            [self changeStatus:@"downloadvedio.plist"];
        }
        
        if (self.isFile) {
            [self changeStatus:@"downloadfile.plist"];
        }

        [self.tableView reloadData];
        
        //清除core data 因为现在还没有适合下拉刷新和适合本地缓存的api
        
        [refreshView endRefreshing];
        PullUpRefreshTimes++;
        
    } failure:^(NSError *error) {
        [refreshView endRefreshing];
        [Alert showAlert:@"上拉刷新失败!"];
    }];
}

-(void)downloadDataWithRequestData:(NSDictionary*)RequestData EntityName:(NSString *)EntityName Mapping:(NSDictionary*)Mapping{
    [PublicMethod ClearEntity:EntityName];
    [NetworkCenter RKRequestWithData:RequestData EntityName:EntityName Mapping:Mapping SuccessBlock:^(NSArray *resultArray) {
        NSMutableArray * dic = [[NSMutableArray alloc] init];
        [dic addObjectsFromArray:resultArray];
        tableViewEntitiesArray = [[NSArray alloc] initWithArray:dic];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"加载失败");
        [Alert showAlert:@"加载失败!"];
    }];
}

/*最新添加*/
-(void)changeStatus:(NSString *)fileName {
    NSLog(@"改变视频文件的下载状态!");
    NSArray * array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * filePath =[array[0] stringByAppendingPathComponent:fileName];
    NSLog(@"filePath = %@",filePath);
    NSFileManager * fileManager = [NSFileManager defaultManager ];
    if (![fileManager fileExistsAtPath:filePath]) {
        NSLog(@"视频下载列表文件不存在!");
    }else{
        NSMutableArray * dict = [NSMutableArray arrayWithContentsOfFile:filePath];
        for (int i = 0; i < tableViewEntitiesArray.count; i ++) {
            if (self.isVideo) {
                PartyVedioDownloadEntity * entity = tableViewEntitiesArray[i];
                if ([dict containsObject:[NSString stringWithFormat:@"%@",entity.theId ]]) {
                    entity.isDownload = @1;
                }else{
                    entity.isDownload = @0;
                }
            }
            if (self.isFile) {
                PartyDataLearnEntity * entity = tableViewEntitiesArray[i];
                if ([dict containsObject:[NSString stringWithFormat:@"%@",entity.theId ]]) {
                    entity.isDownload = @1;
                }else{
                    entity.isDownload = @0;
                }
            }
        }
    }
}
/**/

@end
